package action;

import java.io.PrintWriter;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;
import org.infohazard.maverick.ctl.ThrowawayBean2;

import com.jspsmart.upload.SmartUpload;

import java.io.File;   
import java.io.FileInputStream;   
import java.io.FileOutputStream;   
import java.io.IOException;
import java.io.InputStream;   
import java.io.OutputStream; 

import dao.*;
import entity.*;

public class DataXMLAction extends ThrowawayBean2 {
	
	private String dataType;
    private File   uploadFile;  
    private String fileName;
    private String fileType;
        
    public String getDataType() {
		return dataType;
	}
	public void setDataType(String dataType) {
		this.dataType = dataType;
	}
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public String getFileType() {
		return fileType;
	}
	public void setFileType(String fileType) {
		this.fileType = fileType;
	}	
	public File getUploadFile() {
		return uploadFile;
	}
	public void setUpload_file(File uploadFile) {
		this.uploadFile = uploadFile;
	}
	
	//普通用户登录
	@Override
	protected String perform() throws Exception {

		SmartUpload su = new SmartUpload();//新建一个SmartUpload对象
		//su.getRequest().getParameterValues();//取数组值
		fileName = su.getRequest().getParameter("fileName");//取单个参数单个值
		fileType = su.getRequest().getParameter("fileType");
		dataType = su.getRequest().getParameter("dataType");

		System.out.println(fileName+fileType+dataType);
		
		HttpServletRequest request = this.getCtx().getRequest();
		HttpServletResponse response = this.getCtx().getResponse();
		response.setCharacterEncoding("UTF-8");
		//response.setContentType("text/xml;charset=UTF-8");
		PrintWriter writer = response.getWriter();
		writer.write("<DataXML>");
		
		System.out.println("enter action");

		String distPath = request.getSession().getServletContext().getRealPath("") + "\\upload\\" + fileName;  
        System.out.println(distPath);  
        File disFile = new File(distPath);   
        try {   
            FileUtils.copyFile(uploadFile, disFile);   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   


		writer.write("</DataXML>");
		System.out.println("leave action");
		writer.close();

		return SUCCESS;
	}
}
//boolean isMultipart = ServletFileUpload.isMultipartContent(request);
//if (isMultipart) {
// // Create a factory for disk-based file items
// org.apache.commons.fileupload.FileItemFactory factory = new DiskFileItemFactory();
//
// // Create a new file upload handler
// ServletFileUpload upload = new ServletFileUpload(factory);
// // Parse the request
// List /* FileItem */items = upload.parseRequest(request);
//
// // Process the uploaded items
// Iterator iter = items.iterator();
//
// while (iter.hasNext()) {
//  org.apache.commons.fileupload.FileItem item = (org.apache.commons.fileupload.FileItem) iter
//    .next();
//  if (item.isFormField()) {
//   String name = item.getFieldName();
//   String value = item.getString("GBK");
//   //out.println(name + "=" + value);
//   params.put(name.toUpperCase(), value.trim());
//  }
